﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for vattu
/// </summary>
public class vattu
{
	
	public String Mavt {get ; set;}
    public int Soluong { get; set; }
    public int Dongia{ get; set; }
    public int Sotien{get ; set;}
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader dr;
    vattu vt;

    // them vat tu vao gridvew
    public void add(GridView gv, String So_luong, String s)     //s la 1 ma vat hoac 1 chuoi mavt moi mavt cach nhau 1 dau cach
    {
        List<vattu> lst = new List<vattu>();
        lst = new List<vattu>();
        foreach (GridViewRow row in gv.Rows)
        {
            vt = new vattu();
            TextBox t = ((TextBox)row.FindControl("txtsoluong"));
            vt.Mavt = row.Cells[0].Text;
            vt.Soluong = int.Parse(t.Text);
            vt.Dongia = int.Parse(row.Cells[2].Text);
            t = ((TextBox)row.FindControl("txtsotien"));
            vt.Sotien = int.Parse(t.Text);
            lst.Add(vt);

        }
        MatchCollection mc;
        string chuoi = s;
        chuoi = chuoi + " ";
        //tạo pattern
        //luật:cho tìm ra bất cứ những ký tự không phải ký tự khoảng trắng
        //rồi theo sau nó là kí tự khoảng trắng
        string pattern = @"\S+\s";
        Regex myRegex = new Regex(pattern);
        mc = myRegex.Matches(chuoi);
        datasever data = new datasever();
        conn = data.getconnect();
        conn.Open();
        if (mc.Count > 0)   // ton tai nhieu hon 1 mavt trong textbox
        {
            
            for (int j = 0; j < mc.Count; j++)
            {
                //lay don gia de tinh so tien cho vat tu
                cmd = new SqlCommand("sp_tbl_vat_tu_select_dongia", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Mavt", mc[j].Value));
                dr = cmd.ExecuteReader();
                dr.Read();
                vt = new vattu();
                vt.Mavt = mc[j].Value;
                vt.Soluong = int.Parse(So_luong);
                vt.Dongia = int.Parse(dr["Don_gia"].ToString());
                vt.Sotien = vt.Dongia * vt.Soluong;
                lst.Add(vt);
                dr.Close();
            }
        }
        else
        {
            cmd = new SqlCommand("sp_tbl_vat_tu_select_dongia", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Mavt", s));
            dr = cmd.ExecuteReader();
            dr.Read();
            vt = new vattu();
            vt.Mavt = s;
            vt.Soluong = int.Parse(So_luong);
            vt.Dongia = int.Parse(dr["Don_gia"].ToString());
            vt.Sotien = vt.Dongia * vt.Soluong;
            lst.Add(vt);

        }
        conn.Close();
        gv.DataSource = lst;
        gv.DataBind();
    }


    //xoa vat tu dua vao chi so dong bi xoa
    //i so thu tu dong can xoa
    public void delete(GridView gv,int i)
    {
        int j = 0;
        List<vattu> lst=new List<vattu>();
        foreach (GridViewRow row in gv.Rows)
        {

            if (i != j)
            {
                vt = new vattu();
                vt.Mavt = row.Cells[0].Text;
                TextBox t = ((TextBox)row.FindControl("txtsoluong"));
                vt.Soluong = int.Parse(t.Text);
                vt.Dongia = int.Parse(row.Cells[2].Text);
                t = ((TextBox)row.FindControl("txtsotien"));
                vt.Sotien = int.Parse(t.Text);
                lst.Add(vt);
            }
            j++;
        }
        gv.DataSource = lst;
        gv.DataBind();
    }
	
}